home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / mule / mule-util.el.z / mule-util.el
Encoding:
Text File  |  1998-05-21  |  12.2 KB  |  333 lines

  1. ;;; mule-util.el --- Utility functions for multilingual environment (mule)
  2.  
  3. ;; Copyright (C) 1995 Free Software Foundation, Inc.
  4. ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
  5. ;; Copyright (C) 1997 MORIOKA Tomohiko
  6.  
  7. ;; Keywords: mule, multilingual
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. ;; 02111-1307, USA.
  25.  
  26. ;;; Code:
  27.  
  28. ;;; String manipulations while paying attention to multibyte
  29. ;;; characters.
  30.  
  31. ;; That code is pointless in XEmacs/Mule, since our multibyte
  32. ;; representation doesn't leak to Lisp.
  33.  
  34. ;; string-to-sequence, string-to-list, string-to-vector, store-substring,
  35. ;; truncate-string-to-width
  36.  
  37.  
  38. ;;; Nested alist handler.  Nested alist is alist whose elements are
  39. ;;; also nested alist.
  40.  
  41. ;; [Was defsubst]
  42. ;;;###autoload
  43. (defun nested-alist-p (obj)
  44.   "Return t if OBJ is a nesetd alist.
  45.  
  46. Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
  47. any Lisp object, and BRANCHES is a list of cons cells of the form
  48. (KEY-ELEMENT . NESTED-ALIST).
  49.  
  50. You can use a nested alist to store any Lisp object (ENTRY) for a key
  51. sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT.  KEYSEQ
  52. can be a string, a vector, or a list."
  53.   (and obj (listp obj) (listp (cdr obj))))
  54.  
  55. ;;;###autoload
  56. (defun set-nested-alist (keyseq entry alist &optional len branches)
  57.   "Set ENTRY for KEYSEQ in a nested alist ALIST.
  58. Optional 4th arg LEN non-nil means the firlst LEN elements in KEYSEQ
  59.  is considered.
  60. Optional argument BRANCHES if non-nil is branches for a keyseq
  61. longer than KEYSEQ.
  62. See the documentation of `nested-alist-p' for more detail."
  63.   (or (nested-alist-p alist)
  64.       (error "Invalid arguement %s" alist))
  65.   (let ((islist (listp keyseq))
  66.     (len (or len (length keyseq)))
  67.     (i 0)
  68.     key-elt slot)
  69.     (while (< i len)
  70.       (if (null (nested-alist-p alist))
  71.       (error "Keyseq %s is too long for this nested alist" keyseq))
  72.       (setq key-elt (if islist (nth i keyseq) (aref keyseq i)))
  73.       (setq slot (assoc key-elt (cdr alist)))
  74.       (if (null slot)
  75.       (progn
  76.         (setq slot (cons key-elt (list t)))
  77.         (setcdr alist (cons slot (cdr alist)))))
  78.       (setq alist (cdr slot))
  79.       (setq i (1+ i)))
  80.     (setcar alist entry)
  81.     (if branches
  82.     (if (cdr alist)
  83.         (error "Can't set branches for keyseq %s" keyseq)
  84.       (setcdr alist branches)))))
  85.  
  86. ;;;###autoload
  87. (defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
  88.   "Look up key sequence KEYSEQ in nested alist ALIST.  Return the definition.
  89. Optional 1st argument LEN specifies the length of KEYSEQ.
  90. Optional 2nd argument START specifies index of the starting key.
  91. The returned value is normally a nested alist of which
  92. car part is the entry for KEYSEQ.
  93. If ALIST is not deep enough for KEYSEQ, return number which is
  94.  how many key elements at the front of KEYSEQ it takes
  95.  to reach a leaf in ALIST.
  96. Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil
  97.  even if ALIST is not deep enough."
  98.   (or (nested-alist-p alist)
  99.       (error "invalid arguement %s" alist))
  100.   (or len
  101.       (setq len (length keyseq)))
  102.   (let ((i (or start 0)))
  103.     (if (catch 'lookup-nested-alist-tag
  104.       (if (listp keyseq)
  105.           (while (< i len)
  106.         (if (setq alist (cdr (assoc (nth i keyseq) (cdr alist))))
  107.             (setq i (1+ i))
  108.           (throw 'lookup-nested-alist-tag t))))
  109.       (while (< i len)
  110.         (if (setq alist (cdr (assoc (aref keyseq i) (cdr alist))))
  111.         (setq i (1+ i))
  112.           (throw 'lookup-nested-alist-tag t))))
  113.     ;; KEYSEQ is too long.
  114.     (if nil-for-too-long nil i)
  115.       alist)))
  116.  
  117. ;; Coding system related functions.
  118.  
  119. ;;;###autoload
  120. (defun set-coding-system-alist (target-type regexp coding-system
  121.                         &optional operation)
  122.   "Update `coding-system-alist' according to the arguments.
  123. TARGET-TYPE specifies a type of the target: `file', `process', or `network'.
  124.   TARGET-TYPE tells which slots of coding-system-alist should be affected.
  125.   If `file', it affects slots for insert-file-contents and write-region.
  126.   If `process', it affects slots for call-process, call-process-region, and
  127.     start-process.
  128.   If `network', it affects a slot for open-network-process.
  129. REGEXP is a regular expression matching a target of I/O operation.
  130. CODING-SYSTEM is a coding system to perform code conversion
  131.   on the I/O operation, or a cons of coding systems for decoding and
  132.   encoding respectively, or a function symbol which returns the cons.
  133. Optional arg OPERATION if non-nil specifies directly one of slots above.
  134.   The valid value is: insert-file-contents, write-region,
  135.   call-process, call-process-region, start-process, or open-network-stream.
  136. If OPERATION is specified, TARGET-TYPE is ignored.
  137. See the documentation of `coding-system-alist' for more detail."
  138.   (or (stringp regexp)
  139.       (error "Invalid regular expression: %s" regexp))
  140.   (or (memq target-type '(file process network))
  141.       (error "Invalid target type: %s" target-type))
  142.   (if (symbolp coding-system)
  143.       (if (not (fboundp coding-system))
  144.       (progn
  145.         (check-coding-system coding-system)
  146.         (setq coding-system (cons coding-system coding-system))))
  147.     (check-coding-system (car coding-system))
  148.     (check-coding-system (cdr coding-system)))
  149.   (let ((op-list (if operation (list operation)
  150.            (cond ((eq target-type 'file)
  151.               '(insert-file-contents write-region))
  152.              ((eq target-type 'process)
  153.               '(call-process call-process-region start-process))
  154.              (t        ; i.e. (eq target-type network)
  155.               '(open-network-stream)))))
  156.     slot)
  157.     (while op-list
  158.       (setq slot (assq (car op-list) coding-system-alist))
  159.       (if slot
  160.       (let ((chain (cdr slot)))
  161.         (if (catch 'tag
  162.           (while chain
  163.             (if (string= regexp (car (car chain)))
  164.             (progn
  165.               (setcdr (car chain) coding-system)
  166.               (throw 'tag nil)))
  167.             (setq chain (cdr chain)))
  168.           t)
  169.           (setcdr slot (cons (cons regexp coding-system) (cdr slot)))))
  170.     (setq coding-system-alist
  171.           (cons (cons (car op-list) (list (cons regexp coding-system)))
  172.             coding-system-alist)))
  173.       (setq op-list (cdr op-list)))))
  174.  
  175.  
  176. ;;; Composite charcater manipulations.
  177.  
  178. ;;;###autoload
  179. (defun compose-region (start end &optional buffer)
  180.   "Compose characters in the current region into one composite character.
  181. From a Lisp program, pass two arguments, START to END.
  182. The composite character replaces the composed characters.
  183. BUFFER defaults to the current buffer if omitted."
  184.   (interactive "r")
  185.   (let ((ch (make-composite-char (buffer-substring start end buffer))))
  186.     (delete-region start end buffer)
  187.     (insert-char ch nil nil buffer)))
  188.  
  189. ;;;###autoload
  190. (defun decompose-region (start end &optional buffer)
  191.   "Decompose any composite characters in the current region.
  192. From a Lisp program, pass two arguments, START to END.
  193. This converts each composite character into one or more characters,
  194. the individual characters out of which the composite character was formed.
  195. Non-composite characters are left as-is.  BUFFER defaults to the current
  196. buffer if omitted."
  197.   (interactive "r")
  198.   (save-excursion
  199.     (set-buffer buffer)
  200.     (save-restriction
  201.       (narrow-to-region start end)
  202.       (goto-char (point-min))
  203.       (let ((compcharset (get-charset 'composite)))
  204.     (while (< (point) (point-max))
  205.       (let ((ch (char-after (point))))
  206.         (if (eq compcharset (char-charset ch))
  207.         (progn
  208.           (delete-char 1)
  209.           (insert (composite-char-string ch))))))))))
  210.  
  211. ;;;###autoload
  212. (defconst reference-point-alist
  213.   '((tl . 0) (tc . 1) (tr . 2)
  214.     (ml . 3) (mc . 4) (mr . 5)
  215.     (bl . 6) (bc . 7) (br . 8)
  216.     (top-left . 0) (top-center . 1) (top-right . 2)
  217.     (mid-left . 3) (mid-center . 4) (mid-right . 5)
  218.     (bottom-left . 6) (bottom-center . 7) (bottom-right . 8)
  219.     (0 . 0) (1 . 1) (2 . 2)
  220.     (3 . 3) (4 . 4) (5 . 5)
  221.     (6 . 6) (7 . 7) (8 . 8))
  222.   "Alist of reference point symbols vs reference point codes.
  223. Meanings of reference point codes are as follows:
  224.  
  225.     0----1----2 <-- ascent    0:tl or top-left
  226.     |         |            1:tc or top-center
  227.     |         |            2:tr or top-right
  228.     |         |            3:ml or mid-left
  229.     |    4 <--+---- center    4:mc or mid-center
  230.     |         |            5:mr or mid-right
  231. --- 3         5 <-- baseline    6:bl or bottom-left
  232.     |         |            7:bc or bottom-center
  233.     6----7----8 <-- descent    8:br or bottom-right
  234.  
  235. Reference point symbols are to be used to specify composition rule of
  236. the form \(GLOBAL-REF-POINT . NEW-REF-POINT), where GLOBAL-REF-POINT
  237. is a reference point in the overall glyphs already composed, and
  238. NEW-REF-POINT is a reference point in the new glyph to be added.
  239.  
  240. For instance, if GLOBAL-REF-POINT is 8 and NEW-REF-POINT is 1, the
  241. overall glyph is updated as follows:
  242.  
  243.     +-------+--+ <--- new ascent
  244.     |       |  |
  245.     | global|  |
  246.     | glyph |  |
  247. --- |       |  | <--- baseline (doesn't change)
  248.     +----+--+--+
  249.     |    | new |
  250.     |    |glyph|
  251.     +----+-----+ <--- new descent
  252. ")
  253.  
  254. ;; Return a string for char CH to be embedded in multibyte form of
  255. ;; composite character.
  256. (defun compose-chars-component (ch)
  257.   (if (< ch 128)
  258.       (format "\240%c" (+ ch 128))
  259.     (let ((str (char-to-string ch)))
  260.       (if (cmpcharp ch)
  261.       (if (/= (aref str 1) ?\xFF)
  262.           (error "Char %c can't be composed" ch)
  263.         (substring str 2))
  264.     (aset str 0 (+ (aref str 0) ?\x20))
  265.     str))))
  266.  
  267. ;; Return a string for composition rule RULE to be embedded in
  268. ;; multibyte form of composite character.
  269. (defsubst compose-chars-rule (rule)
  270.   (char-to-string (+ ?\xA0
  271.              (* (cdr (assq (car rule) reference-point-alist)) 9)
  272.              (cdr (assq (cdr rule) reference-point-alist)))))
  273.  
  274. ;;;###autoload
  275. (defun compose-chars (first-component &rest args)
  276.   "Return one char string composed from the arguments.
  277. Each argument is a character (including a composite chararacter)
  278. or a composition rule.
  279. A composition rule has the form \(GLOBAL-REF-POINT . NEW-REF-POINT).
  280. See the documentation of `reference-point-alist' for more detail."
  281.   (if (= (length args) 0)
  282.       (char-to-string first-component)
  283.     (let* ((with-rule (consp (car args)))
  284.        (str (if with-rule (concat (vector leading-code-composition ?\xFF))
  285.           (char-to-string leading-code-composition))))
  286.       (setq str (concat str (compose-chars-component first-component)))
  287.       (while args
  288.     (if with-rule
  289.         (progn
  290.           (if (not (consp (car args)))
  291.           (error "Invalid composition rule: %s" (car args)))
  292.           (setq str (concat str (compose-chars-rule (car args))
  293.                 (compose-chars-component (car (cdr args))))
  294.             args (cdr (cdr args))))
  295.       (setq str (concat str (compose-chars-component (car args)))
  296.         args (cdr args))))
  297.       str)))
  298.  
  299. ;;;###autoload
  300. (defun decompose-composite-char (char &optional type with-composition-rule)
  301.   "Convert composite character CHAR to a string containing components of CHAR.
  302. Optional 1st arg TYPE specifies the type of sequence returned.
  303. It should be `string' (default), `list', or `vector'.
  304. Optional 2nd arg WITH-COMPOSITION-RULE non-nil means the returned
  305. sequence contains embedded composition rules if any.  In this case, the
  306. order of elements in the sequence is the same as arguments for
  307. `compose-chars' to create CHAR.
  308. If TYPE is omitted or is `string', composition rules are omitted
  309. even if WITH-COMPOSITION-RULE is t."
  310.   (or type
  311.       (setq type 'string))
  312.   (let* ((len (composite-char-component-count char))
  313.      (i (1- len))
  314.      l)
  315.     (setq with-composition-rule (and with-composition-rule
  316.                     (not (eq type 'string))
  317.                     (composite-char-composition-rule-p char)))
  318.     (while (> i 0)
  319.       (setq l (cons (composite-char-component char i) l))
  320.       (if  with-composition-rule
  321.       (let ((rule (- (composite-char-composition-rule char i) ?\xA0)))
  322.         (setq l (cons (cons (/ rule 9) (% rule 9)) l))))
  323.       (setq i (1- i)))
  324.     (setq l (cons (composite-char-component char 0) l))
  325.     (cond ((eq type 'string)
  326.        (apply 'concat-chars l))
  327.       ((eq type 'list)
  328.        l)
  329.       (t                ; i.e. TYPE is vector
  330.        (vconcat l)))))
  331.  
  332. ;;; mule-util.el ends here
  333.